package p.ithorns.framework.crypto.core;

import org.springframework.stereotype.Service;
import p.ithorns.framework.crypto.utils.Base64Utils;
import p.ithorns.framework.crypto.utils.RSAUtils;


/**
 * RSACryptoImpl
 *
 * @author ithorns
 * @version 1.0
 * @date 2019/5/24 14:55
 */
@Service
public class RSACrypto implements Crypto {

    @Override
    public String encrypt(String content, String encryptKey) throws Exception {
        byte[] encryptData = RSAUtils.encryptByPublicKey(content.getBytes(), encryptKey);
        return Base64Utils.encode(encryptData);
    }

    @Override
    public String decrypt(String content, String decryptKey, String charset) throws Exception {
        return new String(RSAUtils.decryptByPrivateKey(Base64Utils.decode(content), decryptKey), charset);
    }

    private static String privateyKey = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIvXtRK9eYhi9dwk9wwtFQQoPjuuBT2lb4P/8gFQLf130i+qdCjuXJIrcnMKR2VrBmbX0ee+W+M2dOer5EEwqSG7NElalTYXKUaH5A1mlmVe7CTaTcWAB34z92y1t2DHavkVjoetu6AQZ47sV1Z9Y5NeTOJgslOpgzoclJdlSbn1AgMBAAECgYACWfgzfngisPdNYlm9HKzzyvFw83YnGTcM0yrwlraPQoqIPqeBYLCxzIU6JfwZWnlbFVn0OaE16t6AXsWRyw2s+x9u1wb7Zn6NKqMrMvOQ7KH5WdF+deqS1bXyZPE1YsqeMcjSFI7XMTEwH7sDT1+BKXGRqngr93IGvLcrUSRuQQJBANProIqvwi4FmuFo2ck9+/cpKJnEgxGtQAiAjZaYQlxidmQaYGxea5nV2i9eyUKEYh/za8OQ2H0jHpEoyX9JTekCQQCo7hG0/YNdTZBtQaDFH8IZf57p7CgV1h6CoSBszbxiq1wJfmf6/nuKdYYQVik/yxcBUNe1VAW3S59SS70ZocgtAkBi1pSr2klweYDA/MwGhfhMUE/1V6fhyLICxSJESIM631lkWsD6CheZShpW2Zh4g5HYYQmzzoyJ4KVTAnjn+uJxAkAmd0dK6isedMn9RuNf35JoEfpSk7aw+jeqURw8CkDqStDDTKhKq+XfqlEjqSrtXsZXQ1xK6t3+Vdbqe1qM2pzlAkA5zsfFoE4DnDKDQwrERwECIqA8tYprZI5DqoS81/FlD0AWoDL68/G9uJUdOAwIRJWElpP+B9rU1xJPFTi1dKPM";

    private static String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCL17USvXmIYvXcJPcMLRUEKD47rgU9pW+D//IBUC39d9IvqnQo7lySK3JzCkdlawZm19HnvlvjNnTnq+RBMKkhuzRJWpU2FylGh+QNZpZlXuwk2k3FgAd+M/dstbdgx2r5FY6HrbugEGeO7FdWfWOTXkziYLJTqYM6HJSXZUm59QIDAQAB";


    static String content = "{\r\n\"name\": \"andy\", \"code\": \"123\"\r\n}";
    public static void main(String[] args) throws Exception {
       /* Map<String, Object> keyPair = RSAUtils.genKeyPair();

        RSACrypto crypto = new RSACrypto();
        String encryptStr = crypto.encrypt(content, RSAUtils.getPublicKey(keyPair));
        System.out.println("公钥加密========>" + encryptStr);

        String decryptStr = crypto.decrypt(encryptStr, RSAUtils.getPrivateKey(keyPair), "UTF-8");
        System.out.println("私钥解密========>" + decryptStr);

        String sign =  RSAUtils.sign(Base64Utils.decode(encryptStr), RSAUtils.getPrivateKey(keyPair));
        System.out.println("私钥签名========>" + sign);

        Boolean valid = RSAUtils.verify(Base64Utils.decode(encryptStr), RSAUtils.getPublicKey(keyPair), sign);
        System.out.println("公钥验证========>" + valid);*/


        String src = "J+Rahh0yQuow3NAt9eJagBdIm6MJIGBlLN/8M+5PVHtnhNKCqXsg3McD0AOUxoMBHTUYwZCYe2lVbgoF2dhczDDC3pz3oqPTKdUU7mYH2fgnOZThbjDxZlRhfMIV8EUXwUSh17Vj9XiF+CqGhGSMsuzNgbpvOl5EJK1CbUyA1wM=";

        RSACrypto crypto = new RSACrypto();

        //String result = crypto.decrypt(src, privateyKey, "UTF-8");
        //System.out.println(result);

        String encryptStr = crypto.encrypt(content, publicKey);
        System.out.println("公钥加密========>" + encryptStr);
    }

}